<template>
  <div class="app-container">
    <el-form :model="formData" label-width="120px" label-position="left">
      <el-form-item label="区域名称">
        <el-input v-model="formData.name"></el-input>
      </el-form-item>
      <el-form-item label="区域代码">
        <el-input v-model="formData.code"></el-input>
      </el-form-item>
      <el-form-item label="所属省">
          <el-select v-model="formData.provinceId" placeholder="请选择省" @change="provinceChange">
            <el-option
              v-for="item in provinceList"
              :key="item.regionId"
              :label="item.regionName"
              :value="item.regionId">
            </el-option>
          </el-select>
      </el-form-item>
      <el-form-item label="所属市">
          <el-select v-model="formData.cityId" placeholder="请选择市">
            <el-option
              v-for="item in cityList"
              :key="item.regionId"
              :label="item.regionName"
              :value="item.regionId">
            </el-option>
          </el-select>
      </el-form-item>
    </el-form>
    <div class="submit-box">
      <el-button type="primary" :loading="loading" style="width: 200px;" @click.native.prevent="doSubmit">提交</el-button>
    </div>
  </div>
</template>

<script>
  import { create, getChildren } from '@/api/region'
  import { errorMsg, successMsg } from '@/utils/my'

  export default {
    data() {
      return {
        loading: false,
        reqData: {
          parentId: 0,
          level: 1
        },
        formData: {
          name: '',
          code: '',
          provinceId: '',
          cityId: ''
        },
        provinceList: [],
        cityList: []
      }
    },

    created() {
      this.fetchData()
    },

    methods: {

      doSubmit() {
        const that = this
        let d = JSON.parse(JSON.stringify(that.formData))
        if (d.name.length <= 0) {
          return errorMsg('请输入区域名称')
        }
        d.provinceId = d.provinceId.length <= 0 ? 0 : d.provinceId
        d.cityId = d.cityId.length <= 0 ? 0 : d.cityId
        that.loading = true
        create(d).then(res => {
          that.loading = false
          if (res.code != 20000) {
            return errorMsg(res.message)
          }
          successMsg(res.message)
          setTimeout(function() {
            that.$router.push({path: '/admin/region/list'})
          }, 1500)
        })
      },

      fetchData() {
        const that = this
        getChildren(that.reqData).then(res => {
          if (res.code != 20000) {
            return errorMsg(res.message)
          }
          if (that.reqData.level == 1) {
            that.provinceList = res.data
          } else if (that.reqData.level == 2) {
            that.cityList = res.data
          }
        })
      },

      provinceChange(id) {
        this.reqData.parentId = id
        this.reqData.level = 2
        this.fetchData()
      }
    }
  }
</script>

<style>
  .submit-box {
    height: 50px;
    line-height: 50px;
    text-align: center;
    padding-right: 20px;
  }
</style>
